from django.db import models
from db.AbstractModel import AbstractModel
from tinymce.models import HTMLField


# 商品分类表
class Category(AbstractModel):
    cag_name = models.CharField(max_length=30, verbose_name="商品类别")  # 商品类别

    def __str__(self):
        return 'Category[cag_name=%s]' % self.cag_name


class GoodsInfoManager(models.Manager):
    # 获取最热的三个商品
    def get_hot_by_cag(self, cag):
        return self.filter(goods_cag=cag).order_by('-goods_look')[:3]

    # 获取最新的四个商品
    def get_new_by_cag(self, cag):
        return self.filter(goods_cag=cag).order_by('-id')[:4]

    # 获取所有商品中最热的两个商品
    def get_hot_by_all(self):
        return self.all().order_by('-goods_look')[:2]

    # 获取商品列表
    def get_goods_by_cag(self, cag, show):
        if not show:

            return self.filter(goods_cag_id=cag)
        elif show == 'price':

            return self.filter(goods_cag_id=cag).order_by('-goods_price')

        elif show == 'hot':

            return self.filter(goods_cag_id=cag).order_by('-goods_look')


# 商品信息表
class GoodsInfo(AbstractModel):
    goods_name = models.CharField(max_length=30, verbose_name='商品名')  # 商品名称
    goods_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品价格')  # 商品价格
    goods_unit = models.CharField(max_length=10, verbose_name='商品单位')  # 商品单位
    goods_status = models.BooleanField(default=True, verbose_name='商品上架')  # 商品上架
    goods_image = models.ImageField(verbose_name='商品图片')  # 商品图片
    goods_brief = models.CharField(max_length=100, verbose_name='商品简述')  # 商品简述
    goods_desc = HTMLField(verbose_name='商品描述')  # 商品描述
    goods_look = models.IntegerField(default=0, verbose_name='商品浏览量')  # 商品浏览量
    goods_sales = models.IntegerField(default=0, verbose_name='商品销量')  # 商品销量
    goods_cag = models.ForeignKey(Category, verbose_name='所属商品类别')  # 商品类别

    objects = GoodsInfoManager()

    def __str__(self):
        return 'GoodsInfo[goods_name=%s, goods_price=%s, goods_unit=%s, goods_status=%s, goods_image=%s, goods_brief=%s, goods_desc=%s, goods_look=%s, goods_sales=%s, goods_cag=%s]' % (
            self.goods_name, self.goods_price, self.goods_unit, self.goods_status, self.goods_image, self.goods_brief,
            self.goods_desc, self.goods_look, self.goods_sales, self.goods_cag)


# 广告表
class Advertisement(AbstractModel):
    ad_name = models.CharField(max_length=30, verbose_name='广告名')  # 广告位名称
    ad_image = models.ImageField(upload_to='ad/', verbose_name='广告图片')  # 广告位图片
    ad_link = models.CharField(max_length=80, verbose_name='广告位链接')  # 广告位链接

    def __str__(self):
        return 'Advertisement[ad_name=%s, ad_image=%s, ad_link=%s]' % (self.ad_name, self.ad_image, self.ad_link)


# 商品浏览记录表
class GoodsBrowse(AbstractModel):
    goods_name = models.ForeignKey(GoodsInfo, verbose_name='所属商品名')  # 浏览商品
    user_name = models.ForeignKey('users.user', verbose_name='所属顾客')  # 浏览人

    def __str__(self):
        return 'GoodsBrowse[goods_name=%s, user_name=%s]' % (self.goods_name, self.user_name)
